package com.example.japinhands

import android.util.Log
import com.example.japinhands.data.DeformAdj
import com.example.japinhands.data.DeformVerb
import com.example.japinhands.data.Sentence
import com.example.japinhands.data.Word
import com.example.japinhands.data.WordConstant
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlin.concurrent.thread

object JapRepository {

    private const val TAG = "JapRepository"

    private val wordDao = WordDatabase.getDatabase(JApplication.context).wordDao()
    private val adjDao = WordDatabase.getDatabase(JApplication.context).adjDao()
    private val verbDao = WordDatabase.getDatabase(JApplication.context).verbDao()
    private val sentenceDao = WordDatabase.getDatabase(JApplication.context).sentenceDao()

    // 这段逻辑也可以，研究研究为什么
//    fun getAllWords() = flow {
//        val words = withContext(Dispatchers.IO) {
//            wordDao.getAllWords()
//        }
//        emit(words)
//    }.flowOn(Dispatchers.IO)

    fun getAllWords(onGetAllWords: (Array<Word>?) -> Unit) {
        thread {
            val words = wordDao.getAllWords()
            onGetAllWords.invoke(words)
        }
    }

    fun getAllSentences(onGetSentences: (List<Sentence>?) -> Unit) {
        thread {
            val list = sentenceDao.getAllSentences()
            onGetSentences.invoke(list)
        }
    }

    fun getAllDeformAdj() = adjDao.getAllDeformAdj()

    fun querySentenceById(id: Long, onGetSentence: (Sentence?) -> Unit) {
        thread {
            val sentence: Sentence? = sentenceDao.querySentenceById(id)
            onGetSentence.invoke(sentence)
        }
    }

    fun queryWordById(id: Long, onGetWord: (Word?) -> Unit) {
        thread {
            val word: Word? = wordDao.queryWordById(id)
            onGetWord.invoke(word)
        }
    }

//    suspend fun queryDeformAdj(id: Long) = suspendCoroutine { continuation ->
//            val res = adjDao.queryDeformAdjById(id)
////            if (res != null) {
////                continuation.resume(res)
////            } else {
////                continuation.resumeWithException(RuntimeException("null adj"))
////            }
//            continuation.resume(res)
//        }

    suspend fun queryDeformAdj(id: Long) = withContext(Dispatchers.IO) {
        adjDao.queryDeformAdjById(id)
    }


//    suspend fun queryDeformVerb(id: Long) = suspendCoroutine { continuation ->
//        continuation.resume(
//            verbDao.queryDeformVerbById(id)
//        )
//    }
    suspend fun queryDeformVerb(id: Long) = withContext(Dispatchers.IO) {
        verbDao.queryDeformVerbById(id)
}

    fun insertWord(word: Word) = wordDao.insertWord(word)

    fun insertSentence(sentence: Sentence) = sentenceDao.insertSentence(sentence)

    fun insertDeformAdj(deformAdj: DeformAdj) = adjDao.insertDeformAdj(deformAdj)

    fun insertDeformVerb(deformVerb: DeformVerb) = verbDao.insertDeformVerb(deformVerb)

    fun updateWord(word: Word) {
        wordDao.updateWord(word)
    }

    fun updateDeformVerb(verb: DeformVerb) = verbDao.updateDeformVerb(verb)

    fun updateDeformAdj(adj: DeformAdj) = adjDao.updateDeformAdj(adj)

    fun deleteAll() {
        adjDao.deleteDeformAdj()
        verbDao.deleteDeformVerb()
        sentenceDao.deleteSentences()
        wordDao.deleteWords()
//        WordConstant.saveNewKey(0)
        Log.d(TAG, "dropAll, key: ${WordConstant.currSentenceKey}")
    }

}
